/**  
* <p>Title: cleanAreaService.java</p >  
* <p>Description: </p >  
* <p>Company:luo.com</p >  
* @author 魏家洛 
* @date 2019年1月22日  
* @version 1.0  
*/  
package com.pmsystem.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.AccidentMapper;
import com.pmsystem.dao.CleanAreaMapper;
import com.pmsystem.dao.CleanManMapper;
import com.pmsystem.dao.CleanMisMapper;
import com.pmsystem.pojo.Accident;
import com.pmsystem.pojo.CleanArea;
import com.pmsystem.pojo.CleanMan;
import com.pmsystem.pojo.CleanMis;
import com.pmsystem.util.BeanUtil;

/**  
* <p>Title: cleanAreaService</p >  
* <p>Description: </p >  
* @author 魏家洛  
* @date 2019年1月22日  
*/
@Service
public class CleanService {
	@Autowired
	CleanAreaMapper cleanAreaMapper;
	@Autowired
	CleanManMapper cleanManMapper;
	@Autowired
	CleanMisMapper cleanMisMapper;
	
	
	//保洁区域
	/**  
	 * <p>Title: findAreaPage</p >  
	 * <p>Description: 传入页码和页大小获取页码对应保洁区域信息</p >  
	 * @author 魏家洛  
	 */
	public Map findAreaPage(Integer pageNo,Integer pageSize,String condition) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		Map map = new HashMap<>();
		int pageNum = (int)Math.ceil(cleanAreaMapper.selectAreaCount(condition)/(double)pageSize);
		map.put("pageNum",pageNum);
		int start = (pageNo-1)*pageSize;
		map.put("areaPage",cleanAreaMapper.selectAreaPage(start,pageSize,condition));
		return map;
	}
	/**  
	 * <p>Title: findAllArea</p >  
	 * <p>Description:传入条件查询所有区域信息</p >  
	 * @author 魏家洛
	 * @param condition
	 * @return  
	 */  
	public Map<String,Object> findAllArea(String condition) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		Map<String,Object> map = new HashMap<String,Object>();
		List<CleanArea> arealist= cleanAreaMapper.findAllArea(condition);
		map.put("arealist", arealist);
		map.put("size", arealist.size());
		return map;
	}
	/**  
	 * <p>Title: deleteArea</p >  
	 * <p>Description: 删除保洁区域信息</p >  
	 * @author 魏家洛
	 */  
	public boolean deleteArea(Integer id) {
		int row = cleanAreaMapper.deleteAreaById(id);
		if(row==1) 
			return true;
		else
			return false;
	}
	/**
	 * <p>Title: selectAreaById</p >  
	 * <p>Description:通过保洁区域id查找保洁区域信息 </p >  
	 * @author 魏家洛
	 * @param id
	 * @return
	 */
	public CleanArea selectArea(Integer id) {
		return cleanAreaMapper.selectAreaById(id);
	}
	/**  
	 * <p>Title: updateArea</p >  
	 * <p>Description:更新保洁区域信息</p >  
	 * @author 魏家洛
	 * @param cleanArea  
	 */  
	public boolean updateArea(CleanArea cleanArea) {
		return (cleanAreaMapper.updateAreaByArea(cleanArea)==1);
	}
	/**  
	 * <p>Title: addArea</p >  
	 * <p>Description:添加保洁区域信息 </p >  
	 * @author 魏家洛
	 * @param cleanAreaName  
	 */  
	public boolean addArea(String cleanAreaName) {
		if(BeanUtil.isValid(cleanAreaName))
			return(cleanAreaMapper.addArea(cleanAreaName)==1);
		else
			return false;
	}
	
	
	//保洁人员
	/**  
	 * <p>Title: findManPage</p >  
	 * <p>Description:传入页码和页大小获取页码对应保洁人员信息</p >  
	 * @author 魏家洛
	 * @param pageNo
	 * @param pageSize
	 * @return  
	 */  
	public Map findManPage(Integer pageNo, Integer pageSize,String condition) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		Map map = new HashMap<>();
		int pageNum = (int)Math.ceil(cleanManMapper.selectManCount(condition)/(double)pageSize);
		map.put("pageNum",pageNum);
		int start = (pageNo-1)*pageSize;
		map.put("manPage",cleanManMapper.selectManPage(start,pageSize,condition));
		return map;
	}
	/**  
	 * <p>Title: findAllMan</p >  
	 * <p>Description: 传入条件查询所有人员信息</p >  
	 * @author 魏家洛
	 * @param condition
	 * @return  
	 */  
	public Map<String, Object> findAllMan(String condition) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		Map<String, Object> map = new HashMap<>();
		List<CleanMan> listman = cleanManMapper.findAllMan(condition);
		map.put("size",listman.size());
		map.put("manlist", listman);
		return map;
	}
	/**  
	 * <p>Title: deleteMan</p >  
	 * <p>Description:删除保洁人员信息 </p >  
	 * @author 魏家洛
	 * @param id  
	 */  
	public boolean deleteMan(Integer id) {
		int row = cleanManMapper.deleteManById(id);
		if(row==1) 
			return true;
		else
			return false;
	}
	/**  
	 * <p>Title: selectMan</p >  
	 * <p>Description: 通过保洁人员id查找保洁人员信息</p >  
	 * @author 魏家洛
	 * @param id
	 * @return  
	 */  
	public CleanMan selectMan(Integer id) {
		return cleanManMapper.selectManById(id);
	}
	/**  
	 * <p>Title: updateMan</p >  
	 * <p>Description:更新保洁人员信息 </p >  
	 * @author 魏家洛
	 * @param cleanMan  
	 */  
	public boolean updateMan(CleanMan cleanMan) {
		return (cleanManMapper.updateManByMan(cleanMan)==1);
	}
	/**  
	 * <p>Title: addMan</p >  
	 * <p>Description:添加保洁人员信息 </p >  
	 * @author 魏家洛
	 * @param cleanMan  
	 */  
	public boolean addMan(CleanMan cleanMan) {
		return(cleanManMapper.addMan(cleanMan)==1);
	}
	
	
	
	//保洁任务
	/**  
	 * <p>Title: findMisPage</p >  
	 * <p>Description:传入页码和页大小获取页码对应保洁任务信息</p >  
	 * @author 魏家洛
	 * @param pageNo
	 * @param pageSize
	 * @return  
	 */  
	public Map findMisPage(Integer pageNo, Integer pageSize,String condition,String startTime,String endTime) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		if(!BeanUtil.isValid(startTime))
			startTime=null;
		if(!BeanUtil.isValid(endTime))
			endTime=null;
		Map map = new HashMap<>();
		int pageNum = (int)Math.ceil(cleanMisMapper.selectMisCount(condition,startTime,endTime)/(double)pageSize);
		int start = (pageNo-1)*pageSize;
		map.put("misPage",cleanMisMapper.selectMisPage(start,pageSize,condition,startTime,endTime));
		return map;
	}
	/**  
	 * <p>Title: findAllMis</p >  
	 * <p>Description::查找符合条件的所有保洁任务信息</p >  
	 * @author 魏家洛
	 * @param condition
	 * @param startTime
	 * @param endTime
	 * @return  
	 */  
	public Map<String, Object> findAllMis(String condition, String startTime, String endTime) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		if(!BeanUtil.isValid(startTime))
			startTime=null;
		if(!BeanUtil.isValid(endTime))
			endTime=null;
		Map<String, Object> map = new HashMap<>();
		List<Map<String, Object>> mislist = cleanMisMapper.findAllMis(condition,startTime,endTime);
		map.put("size", mislist.size());
		map.put("mislist", mislist);
		return map;
	}
	/**  
	 * <p>Title: deleteMis</p >  
	 * <p>Description:删除保洁任务信息 </p >  
	 * @author 魏家洛
	 * @param id  
	 */  
	public boolean deleteMis(Integer id) {
		int row = cleanMisMapper.deleteMisById(id);
		if(row==1) 
			return true;
		else
			return false;
	}
	/**  
	 * <p>Title: selectMis</p >  
	 * <p>Description: 通过保洁任务id查找保洁任务信息</p >  
	 * @author 魏家洛
	 * @param id
	 * @return  
	 */  
	public CleanMis selectMis(Integer id) {
		return cleanMisMapper.selectMisById(id);
	}
	/**  
	 * <p>Title: updateMis</p >  
	 * <p>Description:更新保洁任务信息 </p >  
	 * @author 魏家洛
	 * @param cleanMis
	 */  
	public boolean updateMis(CleanMis cleanMis) {
		return (cleanMisMapper.updateMisByMis(cleanMis)==1);
	}
	/**  
	 * <p>Title: addMis</p >  
	 * <p>Description:添加保洁任务信息 </p >  
	 * @author 魏家洛
	 * @param cleanMis 
	 */  
	public boolean addMis(CleanMis cleanMis) {
		if(cleanAreaMapper.selectAreaById(cleanMis.getCleanAreaId())==null &&
		   cleanManMapper.selectManById(cleanMis.getCleanManId())==null)
			return false;
		else
			return(cleanMisMapper.addMis(cleanMis)==1);
	}
	/**  
	 * <p>Title: beFinish</p >  
	 * <p>Description:将任务标记为已完成</p > 
	 * @author 魏家洛
	 * @param id
	 * @return  
	 */  
	public boolean beFinish(Integer id) {
		return (cleanMisMapper.beFinish(id)==1);
	}
	/**  
	 * <p>Title: getProgress</p >  
	 * <p>Description:获取当天任务进度</p >  
	 * @author 魏家洛
	 * @return  
	 */  
	public int getProgress(String currentDate) {
		Float progress = cleanMisMapper.getProgress(currentDate);
		if(progress==null)
			return 100;
		else
			return (int)(progress+0);
	}
	
	
	
}
